Count objects in filtered view

I need to load a view which has a complex filter and count the number of visible objects. Why doesn't the following code work (It counts the number of objects in the module ignoring the filter)?

Thanks,
Ken.

Module m = read(fullName(i), false)
load(view "Myview")
Object o 
int n = 0 
for o in current Module do {n++} 
print n
close(m, false)

mcnairk - Fri Jan 06 11:46:29 EST 2012

Re: Count objects in filtered view
smarti.sj - Fri Jan 06 11:51:44 EST 2012

I believe it is because you are not displaying the module, change the param to true and it should work.

Re: Count objects in filtered view
BDavenport - Fri Jan 06 12:39:19 EST 2012

One alternative I would suggest is to potentially approach this query from the prospective of the complex filter rather than the view. After all, the view is just a collection of attributes to display, it's the filter that identifies the content.

The Filter command has built in variables for accepted and rejected counts. You can construct a complex Filter via DXL, apply it and then display or assign the values for accepted/rejected as you see fit.

For example:

Module mod = current Module
int total_accepted = 0
int total_rejected = 0
 
Filter Reqt = (attribute "Requirement?" == "True")
Filter Version = (attribute "Version" == "1")
Filter Effectivity = (attribute "Effectivity" == "1.5")
 
Filter Ver1Effcount = (Reqt && Version && Effectivity)
 
set(current Module, Ver1Effcount, total_accepted, total_rejected)

 


Then you can either print/display the value for total_accepted or total_rejected or you can assign it to something like a module level attribute.

Using the above, one thing I've done is create upwards of 60 module level attributes that produce "metric" type counts based on complex filters. I create the filters, set the filters, and assign the count results to module level attributes. I copy those module level attributes to approximately 40 requirement modules then I create a module where I pull in those module attributes from each individual module to have one centralized and up to date (every time you load the module or refresh the DXL in the view) module with a large number of metrics for easy access by anybody at any time.

 

Re: Count objects in filtered view
mcnairk - Fri Jan 06 12:50:35 EST 2012

smarti.sj - Fri Jan 06 11:51:44 EST 2012
I believe it is because you are not displaying the module, change the param to true and it should work.

That works just dandy! I'm not sure why I didn't think of it, other than today has been a long week...

Re: Count objects in filtered view
mcnairk - Fri Jan 06 14:23:01 EST 2012

BDavenport - Fri Jan 06 12:39:19 EST 2012

One alternative I would suggest is to potentially approach this query from the prospective of the complex filter rather than the view. After all, the view is just a collection of attributes to display, it's the filter that identifies the content.

The Filter command has built in variables for accepted and rejected counts. You can construct a complex Filter via DXL, apply it and then display or assign the values for accepted/rejected as you see fit.

For example:

Module mod = current Module
int total_accepted = 0
int total_rejected = 0
 
Filter Reqt = (attribute "Requirement?" == "True")
Filter Version = (attribute "Version" == "1")
Filter Effectivity = (attribute "Effectivity" == "1.5")
 
Filter Ver1Effcount = (Reqt && Version && Effectivity)
 
set(current Module, Ver1Effcount, total_accepted, total_rejected)

 


Then you can either print/display the value for total_accepted or total_rejected or you can assign it to something like a module level attribute.

Using the above, one thing I've done is create upwards of 60 module level attributes that produce "metric" type counts based on complex filters. I create the filters, set the filters, and assign the count results to module level attributes. I copy those module level attributes to approximately 40 requirement modules then I create a module where I pull in those module attributes from each individual module to have one centralized and up to date (every time you load the module or refresh the DXL in the view) module with a large number of metrics for easy access by anybody at any time.

 

What you suggest is the more desirable method (and my long term goal); however, I am currently reliant on canned views which may change. I'd then need to update my scripts accordingly. I'm trying to avoid the situation where the filters in my scripts are out of sync and give different bean counts than the views...

Many thanks,
Ken.

Re: Count objects in filtered view
llandale - Mon Jan 09 18:19:36 EST 2012

You need "current = m" before "load(view "Myview")". If the module is already open I don't think the "read" command sets it current. And yes you cannot load a view in an invisible module.

But wait!!

const string    cl_vStandard    = "Standard view"
//*****************************************
bool    fView_LoadView(Module p_mod, string p_NameView)
{     // Load the specified view in the open module, which can be either visible or invisible
        // When NameView is null, presume to load the Standard view
        // Always presume to ignore manual view changes if any.
 
        // Calling programs may want to do this:
        //      for NameView in view(mod) do
        //      {  if (!fView_LoadView(mod, NameView)) continue
        //         for col in mod do
        //         {  deal with columns
        //         }
        //      }     // end views in the Module
 
        // Odd realities:
        // When the module is already Visible, then you
        //      can 'load' the view, but re-opening the module does NOT load any view
        // When the module is already INvisible, then you
        //      cannot 'load' the view, but re-opening the module does indeed load
        //      either the default view or the standard view.
        // Thus, when invisible we temporarily change the default view for
        //      the user to the desired View, and change it back after
        //      reading the module, which loads the view.
        // Also, when the standard view is loaded in this way, the
        //      name of the current view doesn't change, but the columns do.
 
 
        if (null p_mod                  or
            type(p_mod) != "Formal")          return(false)
 
        string  NameToLoad, NameModFull, NameDefault, ErrMess
        Item    itm
        View    viu
        bool    LoadOK
        Module  mod1
 
 
        if (null p_NameView     or
            p_NameView == cl_vStandard) //-
        then NameToLoad = cl_vStandard
        else NameToLoad = p_NameView
 
        NameModFull     = fullName(p_mod)
        itm     = item(NameModFull)
        viu     = view(itm, NameToLoad) // Gets handle even if view doesn't exist
 
        if    (null viu or
               !canRead(viu))
           LoadOK       = false // No such view, or user cannot see it.
        elseif(isVisible(p_mod))
        {  noError()
           LoadOK       = load(p_mod, viu, false)
           ErrMess      = lastError()
        }
        elseif(NameToLoad == cl_vStandard)
        {             // ** Invisible Mod, loading the Standard view
           LoadOK       = p_mod == (Module read(NameModFull, false, true))      // Open, option to read Standard View
        }
        else
        {            // ** Invisible Mod, loading a user view
           NameDefault  = getDefaultViewForUser(p_mod)
           ErrMess      = setDefaultViewForUser(p_mod, p_NameView)
           if (!null ErrMess)   //-
           then LoadOK  = false // No such view
           else
           {  read(NameModFull, false, false)        // Invis, don't load standard view
              if (null NameDefault)     //-
              then clearDefaultViewForUser(p_mod)
              else setDefaultViewForUser  (p_mod, NameDefault)
              LoadOK    = currentView(p_mod) == p_NameView
           }
        }    // end invisible mod loading named view
 
print "fView_LoadView; " LoadOK "\tName [" p_NameView "]:  tcanRead " (canRead viu) "\tToLoad [" NameToLoad "]\n"
print "\tin module '" NameModFull "'\n"
        return(LoadOK)
}    // end fView_LoadView()


-Louie

Re: Count objects in filtered view
mcnairk - Tue Jan 10 09:17:19 EST 2012

llandale - Mon Jan 09 18:19:36 EST 2012

You need "current = m" before "load(view "Myview")". If the module is already open I don't think the "read" command sets it current. And yes you cannot load a view in an invisible module.

But wait!!

const string    cl_vStandard    = "Standard view"
//*****************************************
bool    fView_LoadView(Module p_mod, string p_NameView)
{     // Load the specified view in the open module, which can be either visible or invisible
        // When NameView is null, presume to load the Standard view
        // Always presume to ignore manual view changes if any.
 
        // Calling programs may want to do this:
        //      for NameView in view(mod) do
        //      {  if (!fView_LoadView(mod, NameView)) continue
        //         for col in mod do
        //         {  deal with columns
        //         }
        //      }     // end views in the Module
 
        // Odd realities:
        // When the module is already Visible, then you
        //      can 'load' the view, but re-opening the module does NOT load any view
        // When the module is already INvisible, then you
        //      cannot 'load' the view, but re-opening the module does indeed load
        //      either the default view or the standard view.
        // Thus, when invisible we temporarily change the default view for
        //      the user to the desired View, and change it back after
        //      reading the module, which loads the view.
        // Also, when the standard view is loaded in this way, the
        //      name of the current view doesn't change, but the columns do.
 
 
        if (null p_mod                  or
            type(p_mod) != "Formal")          return(false)
 
        string  NameToLoad, NameModFull, NameDefault, ErrMess
        Item    itm
        View    viu
        bool    LoadOK
        Module  mod1
 
 
        if (null p_NameView     or
            p_NameView == cl_vStandard) //-
        then NameToLoad = cl_vStandard
        else NameToLoad = p_NameView
 
        NameModFull     = fullName(p_mod)
        itm     = item(NameModFull)
        viu     = view(itm, NameToLoad) // Gets handle even if view doesn't exist
 
        if    (null viu or
               !canRead(viu))
           LoadOK       = false // No such view, or user cannot see it.
        elseif(isVisible(p_mod))
        {  noError()
           LoadOK       = load(p_mod, viu, false)
           ErrMess      = lastError()
        }
        elseif(NameToLoad == cl_vStandard)
        {             // ** Invisible Mod, loading the Standard view
           LoadOK       = p_mod == (Module read(NameModFull, false, true))      // Open, option to read Standard View
        }
        else
        {            // ** Invisible Mod, loading a user view
           NameDefault  = getDefaultViewForUser(p_mod)
           ErrMess      = setDefaultViewForUser(p_mod, p_NameView)
           if (!null ErrMess)   //-
           then LoadOK  = false // No such view
           else
           {  read(NameModFull, false, false)        // Invis, don't load standard view
              if (null NameDefault)     //-
              then clearDefaultViewForUser(p_mod)
              else setDefaultViewForUser  (p_mod, NameDefault)
              LoadOK    = currentView(p_mod) == p_NameView
           }
        }    // end invisible mod loading named view
 
print "fView_LoadView; " LoadOK "\tName [" p_NameView "]:  tcanRead " (canRead viu) "\tToLoad [" NameToLoad "]\n"
print "\tin module '" NameModFull "'\n"
        return(LoadOK)
}    // end fView_LoadView()


-Louie

Thanks, I'll give this a try!

Re: Count objects in filtered view
llandale - Tue Jan 10 15:05:51 EST 2012

mcnairk - Tue Jan 10 09:17:19 EST 2012
Thanks, I'll give this a try!

I forgot to mention that this function is still in my "test" pile and haven't tried it for a year or so. The concept is sound but it may need a tweak or two.

Re: Count objects in filtered view
SystemAdmin - Mon Nov 19 13:23:58 EST 2012

BDavenport - Fri Jan 06 12:39:19 EST 2012

One alternative I would suggest is to potentially approach this query from the prospective of the complex filter rather than the view. After all, the view is just a collection of attributes to display, it's the filter that identifies the content.

The Filter command has built in variables for accepted and rejected counts. You can construct a complex Filter via DXL, apply it and then display or assign the values for accepted/rejected as you see fit.

For example:

Module mod = current Module
int total_accepted = 0
int total_rejected = 0
 
Filter Reqt = (attribute "Requirement?" == "True")
Filter Version = (attribute "Version" == "1")
Filter Effectivity = (attribute "Effectivity" == "1.5")
 
Filter Ver1Effcount = (Reqt && Version && Effectivity)
 
set(current Module, Ver1Effcount, total_accepted, total_rejected)

 


Then you can either print/display the value for total_accepted or total_rejected or you can assign it to something like a module level attribute.

Using the above, one thing I've done is create upwards of 60 module level attributes that produce "metric" type counts based on complex filters. I create the filters, set the filters, and assign the count results to module level attributes. I copy those module level attributes to approximately 40 requirement modules then I create a module where I pull in those module attributes from each individual module to have one centralized and up to date (every time you load the module or refresh the DXL in the view) module with a large number of metrics for easy access by anybody at any time.

 

Hi, this is working great for me and I have modified it to count several filters that I have. However, I have one question, is there a way to count the number of objects with/without a link using something like this script? Having a link or not having a link is not a attribute to create a simple filter on.

For example I would like to create a filter to Filter on SHALL statements and count the number that have links. It would be great if I can make it something like Filter hasLinksCount = (SHALL && hasLinks)

Thanks.

Re: Count objects in filtered view
Peter_Albert - Tue Nov 20 03:58:26 EST 2012

SystemAdmin - Mon Nov 19 13:23:58 EST 2012
Hi, this is working great for me and I have modified it to count several filters that I have. However, I have one question, is there a way to count the number of objects with/without a link using something like this script? Having a link or not having a link is not a attribute to create a simple filter on.

For example I would like to create a filter to Filter on SHALL statements and count the number that have links. It would be great if I can make it something like Filter hasLinksCount = (SHALL && hasLinks)

Thanks.

Filter hasLinks(LinkFilter value, string linkModName)

Re: Count objects in filtered view
SystemAdmin - Tue Nov 20 09:04:42 EST 2012

Peter_Albert - Tue Nov 20 03:58:26 EST 2012

Filter hasLinks(LinkFilter value, string linkModName)

Thanks Peter, I ended up finding it in the DXL reference manual and doing it the same way.